C#

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C# >

C#_C#实现保存文件时重名自动生成新文件的方法,本文实例讲述了C#实现保存文

来源:网络整理  作者:网友投稿  发布时间:2020-12-26 23:15
C#实现保存文件时重名自动生成新文件的方法,本文实例讲述了C#实现保存文件时重名自动生成新文件的方法。分享给大...

将一个文档保存为 a.txt 时,发现此文件已经存在,则自动保存为 a(1).txt

/// <summary> /// Generates a new path for duplicate filenames. /// </summary> /// <param>The path.</param> /// <returns></returns> private string GetNewPathForDupes( string path ) { string directory = Path.GetDirectoryName( path ); string filename = Path.GetFileNameWithoutExtension( path ); string extension = Path.GetExtension( path ); int counter = 1; string newFullPath; do { string newFilename = "{0}({1}).{2}".FormatWith( filename, counter, extension ); newFullPath = Path.Combine( directory, newFilename ); counter++; } while ( System.IO.File.Exists( newFullPath ) ); return newFullPath; }

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/c/9579.shtml

最新文章
热门文章
Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C#_C#实现保存文件时重名自动生成新文件的方法,本文实例讲述了C#实现保存文

2020-12-26 编辑:网友投稿

将一个文档保存为 a.txt 时,发现此文件已经存在,则自动保存为 a(1).txt

/// <summary> /// Generates a new path for duplicate filenames. /// </summary> /// <param>The path.</param> /// <returns></returns> private string GetNewPathForDupes( string path ) { string directory = Path.GetDirectoryName( path ); string filename = Path.GetFileNameWithoutExtension( path ); string extension = Path.GetExtension( path ); int counter = 1; string newFullPath; do { string newFilename = "{0}({1}).{2}".FormatWith( filename, counter, extension ); newFullPath = Path.Combine( directory, newFilename ); counter++; } while ( System.IO.File.Exists( newFullPath ) ); return newFullPath; }

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/c/9579.shtml

相关文章

风云图片

推荐阅读

返回C#频道首页